
package com.jade.linkong.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.jade.common.annotation.Log;
import com.jade.common.core.controller.BaseController;
import com.jade.common.core.domain.AjaxResult;
import com.jade.common.core.page.TableDataInfo;
import com.jade.common.enums.BusinessType;
import com.jade.common.utils.StringUtils;
import com.jade.common.utils.poi.ExcelUtil;
import com.jade.framework.util.ShiroUtils;
import com.jade.linkong.domain.EcharDataEntry;
import com.jade.linkong.domain.LkMatterProject;
import com.jade.linkong.domain.LkProject;
import com.jade.linkong.service.ILkMatterProjectService;
import com.jade.linkong.service.impl.LkProjectServiceImpl;
import com.jade.system.domain.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;


/**
 * 事项Controller
 * 
 * @author jade
 * @date 2020-07-15
 */
@Controller
@RequestMapping("/linkong/matterProject")
public class LkMatterProjectController extends BaseController
{
    private String prefix = "linkong/matterProject";

    private static final Logger log = LoggerFactory.getLogger(LkMatterProjectController.class);

    @Autowired
    private ILkMatterProjectService lkMatterProjectService;
    @Autowired
    private LkProjectServiceImpl projectService;


    @RequiresPermissions("linkong:matterProject:view")
    @GetMapping("/view")
    public String project(String tab,ModelMap mmap)
    {
        mmap.put("tab",tab);
        return prefix + "/project";
    }

    /**
     * 查询事项列表
     */
    @RequiresPermissions("linkong:matterProject:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LkMatterProject lkMatterProject)
    {
        startPage();
        List<LkMatterProject> list = lkMatterProjectService.selectLkMatterProjectList(lkMatterProject);
        return getDataTable(list);
    }

    /**
     * 导出事项列表
     */
    @RequiresPermissions("linkong:matterProject:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LkMatterProject lkMatterProject)
    {
        List<LkMatterProject> list = lkMatterProjectService.selectLkMatterProjectList(lkMatterProject);
        ExcelUtil<LkMatterProject> util = new ExcelUtil<LkMatterProject>(LkMatterProject.class);
        return util.exportExcel(list, "project");
    }

    /**
     * 新增事项
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存事项
     */
    @RequiresPermissions("linkong:matterProject:add")
    @Log(title = "事项", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LkMatterProject lkMatterProject)
    {
        return toAjax(lkMatterProjectService.insertLkMatterProject(lkMatterProject));
    }

    /**
     * 修改事项
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id ,Long projectId, ModelMap mmap)
    {
        LkMatterProject lkMatterProject = lkMatterProjectService.selectLkMatterProjectById(id,projectId);
        mmap.put("lkMatterProject", lkMatterProject);
        return prefix + "/edit";
    }

    /**
     * 修改保存事项
     */
    @RequiresPermissions("linkong:matterProject:edit")
    @Log(title = "事项", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LkMatterProject lkMatterProject)
    {
        return toAjax(lkMatterProjectService.updateLkMatterProject(lkMatterProject));
    }
    
    /**
     * 上传视图
     * @return
     */
    @GetMapping("/uploadView/{id}")
    public String uploadView(@PathVariable("id") Long id,Long projectId, ModelMap mmap)
    {
        LkMatterProject lkMatterProject = lkMatterProjectService.selectLkMatterProjectById(id,projectId);
        mmap.put("lkMatterProject", lkMatterProject);
        return prefix + "/upFile";
    }

    /**
     * 上传文件
     * @return
     * @throws IOException 
     */
    @PostMapping("/uploadFlie")
    @ResponseBody
    public AjaxResult uploadFlie(String fileName, Long id,Long projectId)
    {
        if (StringUtils.isNotEmpty(fileName)){
            LkMatterProject matterProject = lkMatterProjectService.selectLkMatterProjectById(id,projectId);
            matterProject.setFilePath(fileName);
            return toAjax(lkMatterProjectService.updateLkMatterProject(matterProject));
        }else {
			return AjaxResult.error("请先选择上传的文件");
		}
    }

    /**
     * 事项指南
     */
    @GetMapping("/guideView/{id}")
    public String guideView(@PathVariable("id") Long id,Long projectId, ModelMap mmap)
    {
        LkMatterProject matterProject = lkMatterProjectService.selectLkMatterProjectById(id,projectId);
        mmap.put("matterProject", matterProject);
        return prefix + "/guideView";
    }

    /**
     * 审核事项
     */
    @GetMapping("/auditStatus/{id}")
    public String auditStatus(@PathVariable("id") Long id,Long projectId, ModelMap mmap)
    {
        LkMatterProject lkMatterProject = lkMatterProjectService.selectLkMatterProjectById(id,projectId);
        String projectNamw = projectService.selectLkProjectById(lkMatterProject.getProjectId().toString()).getProName();
        lkMatterProject.setProjectName(projectNamw);
        mmap.put("lkMatterProject", lkMatterProject);
        return prefix + "/editStatus";
    }

    @PostMapping("/getMatterProject")
    @ResponseBody
    public AjaxResult getMatterProject(Long id,Long projectId)
    {
        AjaxResult result = AjaxResult.success();
        LkMatterProject matterProject = lkMatterProjectService.selectLkMatterProjectById(id,projectId);
        result.put("fileName", matterProject.getFilePath());
        return result;
    }

    /**
     * 删除事项
     */
    @RequiresPermissions("linkong:matterProject:remove")
    @Log(title = "事项", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lkMatterProjectService.deleteLkMatterProjectByIds(ids));
    }

    /***
     *  批量插入/修改
     * @Author wupujian
     * @Description
     * @Date  2020/7/18
     * @Param [data]
     * @return com.jade.common.core.domain.AjaxResult
     **/
    @PostMapping( "/saveBatch")
    @ResponseBody
    public AjaxResult saveBatch(String data,String deleteIds,String projectId){
        List<LkMatterProject> list = JSON.parseArray(data, LkMatterProject.class);
        return toAjax(lkMatterProjectService.saveBatch(list,deleteIds,projectId));
    }


    // 首页数据展示===========================================================================

    /***
     * echar数据接口
     * @Author wupujian
     * @Description
     * @Date  2020/7/20
     * @Param []
     * @return com.jade.common.core.domain.AjaxResult
     **/
    @GetMapping("/setectHalfYearMatterCount")
    @ResponseBody
    public AjaxResult setectHalfYearMatterCount(){
        AjaxResult result = new AjaxResult();
        List<EcharDataEntry> sixMonthData = lkMatterProjectService.selectEcharData();
        result.put("result", sixMonthData);
        return result;
    }

    /**
     * 查询事项列表
     */
    @PostMapping("/indexList")
    @ResponseBody
    public TableDataInfo indexList(int pageNum)
    {
        PageHelper.startPage(pageNum, 10, "");
        List<LkMatterProject> list = lkMatterProjectService.getIndexTableData(new LkMatterProject());
        return getDataTable(list);
    }

    /**
     * 企业与责任部门首页事项查询
     * @param pageNum
     * @return
     */
    @PostMapping("/firmIndexList")
    @ResponseBody
    public TableDataInfo firmIndexList(int pageNum)
    {
        PageHelper.startPage(pageNum, 10, "");
        String roleKey = ShiroUtils.getSysUser().getRoleKey();
        Long id = null;
        if ( StringUtils.isNotNull(roleKey) && roleKey.equals("company")){
            id = ShiroUtils.getSysUser().getUserId();
        }else {
            id = ShiroUtils.getSysUser().getDeptId();
        }
        LkMatterProject matterProject = new LkMatterProject();
        matterProject.setMainOrg(id.toString());
        List<LkMatterProject> lkMatterProjects = lkMatterProjectService.selectLkMatterProjectList(matterProject);
        return getDataTable(lkMatterProjects);
    }

    @PostMapping("/getMoreList")
    @ResponseBody
    public List<LkMatterProject>  getMoreList(LkMatterProject lkMatterProject){
        return lkMatterProjectService.getMoreList(lkMatterProject);
    }

    /***
     *  首页统计饼图
     * @Author wupujian
     * @Description
     * @Date  2020/8/3
     * @Param [matterProject]
     * @return java.util.Map<java.lang.String,java.util.List<com.jade.linkong.domain.LkMatterProject>>
     **/
    @PostMapping("/selectLightGroupByOrg")
    @ResponseBody
    public Map<String, List<LkMatterProject>> selectLightGroupByOrg(LkMatterProject matterProject){
        return lkMatterProjectService.selectLightGroupByOrg(matterProject);
    }
    /**
     * 部门事项信息
     */
    @GetMapping("/deptMore")
    public String deptMore()
    {
        return prefix + "/deptMore";
    }

    @PostMapping("/getDeptMoreList")
    @ResponseBody
    public Map<String,List<LkMatterProject>> getDeptMoreList(){
        Map<String,List<LkMatterProject>> map = lkMatterProjectService.selectListBydept();
        return map;
    }

    /**
     * 跳转事项签收
     * @return
     */
    @GetMapping("/signFor")
    public String signFor(ModelMap modelMap){
        List<LkProject> lkProjects = projectService.selectProjectMoreMatterList();
        modelMap.put("projects", lkProjects);
        return prefix + "/signFor";
    }

    /**
     * 查询未授受的事项
     * @return
     */
    @GetMapping("/acceptingCount")
    @ResponseBody
    public int selectAcceptingCount(){
        LkMatterProject matterProject = new LkMatterProject();

        // 根据角色使用角色id或部门id做为查询条件
        String roleKey = ShiroUtils.getSysUser().getRoleKey();
        Long id = null;
        if ( StringUtils.isNotNull(roleKey) && roleKey.equals("company")){
            id = ShiroUtils.getSysUser().getUserId();
        }else {
            id = ShiroUtils.getSysUser().getDeptId();
        }
        matterProject.setMainOrg(id.toString());
        matterProject.setAcceptingState(1);
        List<LkMatterProject> lkMatterProjects = lkMatterProjectService.selectLkMatterProjectList(matterProject);
        return lkMatterProjects.size();
    }

    /**
     * 修改事项接受状态
     * @return
     */
//    @RequiresPermissions("linkong:matterProject:updateAcceptingState")
    @Log(title = "接受状态", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAcceptingState")
    @ResponseBody
    public AjaxResult updateAcceptingState(){
        String roleKey = ShiroUtils.getSysUser().getRoleKey();
        Long id = null;
        if ( StringUtils.isNotNull(roleKey) && roleKey.equals("company")){
            id = ShiroUtils.getSysUser().getUserId();
        }else {
            id = ShiroUtils.getSysUser().getDeptId();
        }
        return toAjax(lkMatterProjectService.updateAcceptingState(id));
    }



}
